//
//  CircleViewController.m
//  hospitalVIP
//
//  Created by xiaoyan on 14-3-9.
//  Copyright (c) 2014年 MEETStudio. All rights reserved.
//

#import "CircleViewController.h"
#import "CircleTopicViewController.h"
#import  "CityViewController.h"

@interface CircleViewController (){
    
    NSArray *categes;
}

@end

@implementation CircleViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc{
    
    [categes release];
    
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];


    NSString *path = [[NSBundle mainBundle] pathForResource:@"bg_pattern_wood" ofType:@"png"];
    self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:path]];
    
    RecipeSegmentControl *segmentcontrol = [[RecipeSegmentControl alloc] init];
    [self.view addSubview:segmentcontrol];
    [segmentcontrol setDelegate:self];
    [segmentcontrol release];

    self.title = @"圈子";
    
    //ios7 优化
    if( iOS7) {
        
        self.edgesForExtendedLayout = UIRectEdgeNone;
        self.navigationController.navigationBar.translucent = NO;
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbar.png"] forBarMetrics:UIBarMetricsDefault];
        
    }
    else{
       [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbar.png"] forBarMetrics:UIBarMetricsDefault];
        
    }
    
    
    UIButton *leftBt = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
    [leftBt setImage:[UIImage imageNamed:@"icon_return.png"] forState:UIControlStateNormal];
    [leftBt addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithCustomView:leftBt];
    [self.navigationItem setLeftBarButtonItem:leftButton];
    [leftBt release];
    [leftButton release];
    
    
    //
    NSDictionary *dict1 = [NSDictionary dictionaryWithObjectsAndKeys:@"testicon2.jpg",@"picurl",@"官方公告",@"name", nil];
     NSDictionary *dict2 = [NSDictionary dictionaryWithObjectsAndKeys:@"testicon3.jpg",@"picurl",@"向专家提问",@"name", nil];
     NSDictionary *dict3 = [NSDictionary dictionaryWithObjectsAndKeys:@"testicon4.jpg",@"picurl",@"孕妇食谱",@"name", nil];
    NSDictionary *dict4 = [NSDictionary dictionaryWithObjectsAndKeys:@"testicon5.jpg",@"picurl",@"健康测评",@"name", nil];
    
    categes = [[NSArray alloc]initWithObjects:dict1,dict2,dict3,dict4, nil];

   
}

- (void)backAction:(id)sender{
    
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    if (section == 0) {
        return 1;
    }
    return categes.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.backgroundColor = [UIColor clearColor];
        
        
        if (indexPath.section != 0) {
            UIImageView *bgview = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 300,90 )];
            bgview.backgroundColor = [UIColor colorWithRed:192/255.0 green:174/255.0 blue:151/255.0 alpha:1];
            [cell.contentView addSubview:bgview];
            bgview.clipsToBounds = YES;
            [bgview release];
            
            UIImageView *signImageView = [[UIImageView alloc]initWithFrame:CGRectMake(15, 15, 80, 80)];
            signImageView.image = [UIImage imageNamed:@"testicon2.png"];
            signImageView.tag = 7;
            signImageView.clipsToBounds = YES;
            [signImageView setContentMode:UIViewContentModeScaleAspectFill];
            [cell.contentView addSubview:signImageView];
            [signImageView release];
            
            UILabel *nameLable = [[UILabel alloc] initWithFrame:CGRectMake(105, 10, 220, 40)];
            nameLable.tag = 8;
            nameLable.textColor = [UIColor colorWithRed:51.0 / 255.0 green:26.0 / 255.0 blue:3.0 / 255.0 alpha:1.0];
            nameLable.font = [UIFont boldSystemFontOfSize:17];
            nameLable.textAlignment = NSTextAlignmentLeft;
            nameLable.text = @"官方公告";
            [cell.contentView addSubview:nameLable];
            [nameLable release];

            
            UIImageView *subicon1 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"like_top_iphone.png"]];
            subicon1.frame = CGRectMake(105, 50, 14, 12);
            [cell.contentView addSubview:subicon1];
            [subicon1 release];
            
            UILabel *sublable1 = [[UILabel alloc]initWithFrame:CGRectMake(120, 50, 30, 12)];
            sublable1.font = [UIFont systemFontOfSize:10];
            sublable1.backgroundColor = [UIColor clearColor];
            [cell.contentView addSubview:sublable1];
            sublable1.text = @"124";
            [sublable1 release];
            
            
            UIImageView *subicon2 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"said_top_iphone.png"]];
            subicon2.frame = CGRectMake(155, 50, 14, 12);
            [cell.contentView addSubview:subicon2];
            [subicon2 release];
            
            UILabel *sublable2 = [[UILabel alloc]initWithFrame:CGRectMake(170, 50, 30, 12)];
            sublable2.font = [UIFont systemFontOfSize:10];
            sublable2.backgroundColor = [UIColor clearColor];
            [cell.contentView addSubview:sublable2];
            sublable2.text = @"479";
            [sublable2 release];
            
            
            
        }
        
       
    }
    
    if (indexPath.section !=0) {
        UIImageView *imageView = (UIImageView*)[cell.contentView viewWithTag:7];
        NSString *imagename = [[categes objectAtIndex:indexPath.row] objectForKey:@"picurl"];
        imageView.image = [UIImage imageNamed:imagename];
        
        
        UILabel *namelable = (UILabel*)[cell.contentView viewWithTag:8];
        NSString *name = [[categes objectAtIndex:indexPath.row] objectForKey:@"name"];
        namelable.text = name;

    }
       // Configure the cell...
    
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 50;
    }
    else{
        return 100;
    }
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


#pragma mark - Table view delegate

// In a xib-based application, navigation from a table can be handled in -tableView:didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section != 0) {
        
        CircleTopicViewController *detailViewController = [[CircleTopicViewController alloc] initWithNibName:@"CircleTopicViewController" bundle:nil];
        
        // Pass the selected object to the new view controller.
        
        // Push the view controller.
        [self.navigationController pushViewController:detailViewController animated:YES];
        [detailViewController release];

    }
   }
 


#pragma mark  delegate
- (void)segmentButtonSelected:(SegmentButtonView *)highlightedSegmentButton{
    
    switch (highlightedSegmentButton.tag) {
        case 1:
            
            break;
            case 2:
        {
            CityViewController *cityVC=[[CityViewController alloc] init];
            [self.navigationController pushViewController:cityVC animated:YES];
            [cityVC release];
        }

            break;
        default:
            break;
    }
    
}


@end
